Repository navigation
perf(document): avoid enumerating Buffer bytes and large values when skipping document array element paths - #16766
Open
harpreetkbadhan wants to merge 1 commit into
Conversation
…skipping document array element paths Re: Automattic#16765 _getPathsToValidate() called subdoc.modifiedPaths({ includeChildren: true }) for every document array element, which expands Buffers byte by byte and large primitive arrays / Mixed objects key by key. Instead, scan the paths to validate once per array and drop the ones under covered elements, so the cost follows the schema rather than the data. Keeps the Automatticgh-16484 behaviour.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #16765.
Since 9.11.0 (#16484), validating a document whose document array elements contain a
Buffer, a large primitive array or a Mixed object with many keys costs time and memory in proportion to the size of that value._getPathsToValidate()calledsubdoc.modifiedPaths({ includeChildren: true })for each element. That expands every modified object value withObject.keys(), which gives one path per byte for aBuffer. A few MB of buffer data in a document array element can run the process out of heap memory duringsave()/validate().This PR replaces that loop with
_deleteDocumentArrayElementDescendants(). When the document array is validated as a whole, the helper scans thepathsset once and removes every entry under one of the array's elements. Thepathsset only holds paths that are actually up for validation, so the cost follows the schema rather than the data. Scanning once per array, rather than once per element, also keeps the cost linear for large arrays. The #16484 behaviour is unchanged: element subpaths, including nested document array paths, are still dropped when the parent array covers them, and the existing tests for it pass.Repro from the issue, before → after:
Examples
Tests added in
test/document.validate.test.jsunderlarge values in document array elements (gh-16765). A 4 MB Buffer, a 100k-element[Number]and a 100k-key Mixed object inside a document array element validate withoutmodifiedPaths()returning per-byte, per-element or per-key paths. There's also a check that element-levelrequirederrors next to a large Buffer are still reported.